package com.uchiha.nova.mainService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.uchiha.nova.core.model.entity.*;
import com.uchiha.nova.core.tool.constant.NovaRoleConstant;
import com.uchiha.nova.core.tool.constant.RedisCacheConstants;
import com.uchiha.nova.mainService.mapper.RoleMapper;
import com.uchiha.nova.core.tool.support.BaseServiceImpl;
import com.uchiha.nova.mainService.mapper.RoleServiceMapper;
import com.uchiha.nova.mainService.mapper.ServiceMapper;
import com.uchiha.nova.mainService.mapper.UserRoleMapper;
import com.uchiha.nova.mainService.service.IRoleService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色管理 服务实现类
 *
 * @author Chill
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleEntity> implements IRoleService {

    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    @Resource
    RoleServiceMapper roleServiceMapper;
    @Resource
    ServiceMapper serviceMapper;
    @Resource
    UserRoleMapper userRoleMapper;


    @Async
    public void init(List<RoleEntity> roleEntities){
        for (int i = 0; i < roleEntities.size(); i++) {
            RoleEntity roleEntity = roleEntities.get(i);
            List<RoleServiceEntity> roleServiceEntities =  roleServiceMapper.selectList(new QueryWrapper<RoleServiceEntity>().eq("role_id",roleEntity.getId()));
            if(roleServiceEntities.size()>0){
                List<ServiceEntity> serviceEntityList = new ArrayList<>();
                List<Long> serviceIds = roleServiceEntities.stream().map(item->item.getServiceId()).collect(Collectors.toList());
                serviceEntityList = serviceMapper.selectBatchIds(serviceIds);

                List<String> services = serviceEntityList.stream().map(item->item.getUrl()).collect(Collectors.toList());
                redisTemplate.opsForValue().set(RedisCacheConstants.ROLE_SERVICE+roleEntity.getRoleName(),services);
                redisTemplate.convertAndSend(RedisCacheConstants.ROLE_SERVICE, roleEntity.getRoleName());
            }

        }
    }

    @Override
    public boolean deletePhysics(List<Long> ids) {
        ids.forEach(item-> {
            Long a = this.baseMapper.selectCount(new QueryWrapper<RoleEntity>().eq("status",1).eq("id",item));
            if(a.compareTo(0L)==0){
                this.baseMapper.deletePhysics(item);
            }
        });
        return true;
    }

    @Override
    public String initLogin(List<UserRoleEntity> userRoleEntities) {
        List<Long> roleIds = userRoleEntities.stream().map(item->item.getRoleId()).collect(Collectors.toList());
        List<RoleEntity> roleEntities = baseMapper.selectBatchIds(roleIds);
        this.init(roleEntities);
        List<String> roleNames = roleEntities.stream().map(item->item.getRoleName()).collect(Collectors.toList());
        return String.join(",",roleNames);
    }

    @Override
    public List<RoleEntity> getUserRoles(Long userId) {
        List<RoleEntity> list = userRoleMapper.selectRole(userId);
        return list;
    }

    @Override
    public List<UserRoleEntity> basicInit(UserEntity userEntity) {
        RoleEntity roleEntity = baseMapper.selectOne(new QueryWrapper<RoleEntity>().eq("role_name", NovaRoleConstant.ROLE_basic));
        List<UserRoleEntity> userRoleEntities = new ArrayList<>();
        UserRoleEntity userRoleEntity = new UserRoleEntity();
        userRoleEntity.setRoleId(roleEntity.getId());
        userRoleEntity.setUserId(userEntity.getId());
        userRoleMapper.insert(userRoleEntity);
        userRoleEntities.add(userRoleEntity);

        return userRoleEntities;
    }

//    @Override
//    public void initTenantAdmin(String tenant) {
//        List<TenantNavMenu> tenantNavMenus = tenantNavMenuMapper.selectList(new QueryWrapper<TenantNavMenu>().eq("tenant_id",tenant));
//        List<Long> ids = tenantNavMenus.stream().map(item->item.getNavMenuId()).collect(Collectors.toList());
//        List<NavMenuEntity> navMenus = navMenuMapper.selectBatchIds(ids);
//        List<NavMenuVO> allNavMenu = navMenus.stream().map(navMenu->{
//            NavMenuVO navMenuVO = BeanUtil.copy(navMenu,NavMenuVO.class);
//            Long a = navMenuMapper.selectCount(new QueryWrapper<NavMenuEntity>().eq("parent_id", navMenu.getId()));
//            if (a.compareTo(0L) == 1) {
//                navMenuVO.setHasChildren(true);
//            } else {
//                navMenuVO.setHasChildren(false);
//            }
//            return navMenuVO;
//        }).collect(Collectors.toList());
//        List<NavMenuVO> listLevelTop = allNavMenu.stream().filter(navMenu->
//                navMenu.getParentId().equals(0L)
//        ).map(item->{
//            item.setChildren(getChildrens(item,allNavMenu));
//            return item;
//        }).collect(Collectors.toList());
//        redisTemplate.opsForValue().set(RedisCacheConstants.NOVA_TENANT_NAV_MENU+tenant,listLevelTop);
//    }

}
